// 手机号验证

function registName() {
	let regName = /^1\d{10}$/;
	if (regName.test(oUseName.value)) {
		oItemspName.style.display = "none";
	} else {
		oItemspName.style.display = "block";
		oItemspName.innerText = "请输入11位正确手机号";
	}
}

// 密码验证
function registPwd() {
	let regPwd = /^\w{8,20}$/;
	if (regPwd.test(oUsePwd.value)) {
		oItemspPwd.style.display = "none";
	} else {
		oItemspPwd.style.display = "block";
		oItemspPwd.innerText = "输入不正确,至少8个字符";
	}
}

// 密码再次验证
function registPwd2() {
	if (oUsePwd2.value != oUsePwd.value) {
		oItemspPwd2.style.display = "block";
		oItemspPwd2.innerText = "输入的密码不一致";
	} else {
		oItemspPwd2.style.display = "none";
	}
}


// 生成验证码
function createCode() {
	let oCreateCet1 = document.querySelector("#reg-create-cet1");
	code = "";
	let codeLength = 4; //验证码的长度   
	let random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
		'N', 'O', 'P', 'Q', 'R',
		'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数  
	for (let i = 0; i < codeLength; i++) { //循环操作  
		let index = Math.floor(Math.random() * 36); //取得随机数的索引（0~35）  
		code += random[index]; //根据索引取得随机数加到code上  
	}
	oCreateCet1.innerText = code; //把code值赋给验证码  
}
// 图形验证码
function validate() {
	if (oNumIpt.value != code) {
		oItemspCet.style.display = "block";
		oItemspCet.innerText = "验证码输入错误";
		createCode();
	} else {
		oItemspCet.style.display = "none";
	}
}

// 手机验证码
function smsPhone() {
	if (oLastIpt.value != "12345") {
		oItemspPhone.style.display = "block";
		oItemspPhone.innerText = "验证码为12345";
	} else {
		oItemspPhone.style.display = "none";
	}
}


// -------------------------------------

let oCodeCreate = document.querySelector(".reg-code-create");
let oCreateCet1 = document.querySelector("#reg-create-cet1");
let oUseName = document.querySelector(".reg-useName");
let oItemspName = document.querySelector("#reg-item-spName");
let oUsePwd = document.querySelector(".reg-pwd");
let oItemspPwd = document.querySelector("#reg-item-spPwd");
let oUsePwd2 = document.querySelector(".reg-pwd2");
let oItemspPwd2 = document.querySelector("#reg-item-spPwd2");
let oNumIpt = document.querySelector(".seg-NumIpt");
let oItemspCet = document.querySelector("#reg-item-spCet");
let oLastIpt = document.querySelector(".seg-lastIpt");
let oItemspPhone = document.querySelector("#reg-item-spPhone");
let oregSms = document.querySelector(".reg-sms");

// 手机号验证
// console.log(typeof oUseName);
oUseName.onblur = function() {
	registName();
}

// 密码验证
oUsePwd.onblur = function() {
	registPwd();
}

// 密码再次验证
oUsePwd2.onblur = function() {
	registPwd2();
}

// 图形验证码
createCode();
oCodeCreate.onclick = function() {
	createCode();
}
oNumIpt.onblur = function() {
	validate();
}

// 短信验证码
oLastIpt.onblur = function() {
	smsPhone();
}

// 注册
$("#reg-policy-check").click(function() {
	if ($("#reg-policy-check")[0].checked == true) {
		$(".reg-ergButton").css({
			opacity: 1,
			cursor: "pointer"
		})
	} else {
		$(".reg-ergButton").css({
			opacity: 0.5,
			cursor: "not-allowed"
		});
	}
});

// 连接php
$(".reg-ergButton").click(function() {
	$.ajax({
		url: "php/userregister.php",
		data: {
			userphone: $(".reg-useName").val(),
			userpwd: $(".reg-pwd").val()
		},
		type: "get",
		success: function(res) {
			if (res == "1") {
				alert("此手机号已注册,请登录");
			} else {
				alert("注册成功");
				location.href = "Home.html";
			}
		}
	});
});
